package com.example.wm.service.impls;


import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
public class OCRService {
    public String extractTextFromImage(File imageFile) {
        Tesseract instance = new Tesseract();
        //设置 tessdata 目录

        // 设置 locale
        System.setProperty("user.language", "C");
        System.setProperty("user.country", "C");
        System.setProperty("user.variant", "C");
//            instance.setDatapath("src/main/resources/tessdata");
        instance.setDatapath("C:\\Program Files\\Tesseract-OCR\\tessdata");
//        instance.setDatapath("/usr/local/share/tessdata");

        //设置语言 中文
        instance.setLanguage("chi_sim");

        try {
            return instance.doOCR(imageFile);
        } catch (TesseractException e) {
            e.printStackTrace();
            return "读取图像时出错";
        }
    }
}
